<template>
<view class="container">
    <form bindsubmit="formSubmit">
        <view class="modify">
            <input placeholder="请填写真实姓名，以免影响提现审核" focus="true" name="truename" bindfocus="inputBg" bindblur="inputBg" :style="'background:' + inputBg" :value="input"></input>
        </view>
        <view class="modify">
            <input type="digit" placeholder="请填写提现金额" name="money" bindfocus="inputBg1" bindblur="inputBg1" :style="'background:' + inputBg1" :value="input"></input>
        </view>
        <view class="remind">金额最多仅支持两位小数,只支持微信提现</view>
        <view class="remind">余额：{{userInfo.now_money}},可提现：{{currency + userInfo.can_withdraw_money}}</view>
        <view class="remind">发起提现申请后，工作人员将会在3-7日内进行审核。</view>
        <button class="block_btn mlr20" formType="submit">提现</button>
    </form>
    <view class="inner" v-if="withdraw_list.length > 1">
        <view class="lists">
            <view class="item">
                <view class="date">提款金额</view>
                <view class="date">状态</view>
                <view class="date">日期</view>
            </view>
            <block v-for="(item, withdraw_list) in withdraw_list" :key="withdraw_list">
                <view class="item">
                    <view class="date">{{item.money}}</view>
                    <view class="date">
                        <text v-if="item.status==0">审核中</text>
                        <text v-else-if="item.status==1">已提现</text>
                        <text v-else-if="item.status==2">取消</text>
                    </view>
                    <view class="date">{{item.add_time}}</view>
                </view> 
            </block>      
        </view>
	</view>
</view>
</template>

<script>
// pages/my/money_withdraw.js
import common from '../../utils/common.js'
var app = getApp();

export default {
  data() {
    return {
      userInfo: {},
      inputBg: 'white',
      inputBg1: 'white',
      withdraw_list: [],
      input: '',
	  currency: '¥'
    };
  },

  onUnload: function () {// 页面关闭
  },
  onHide: function () {// 页面隐藏
  },
  onShow: function () {// 页面显示
  // #ifdef H5
  common.hideWxShare()
  // #endif
  },
  onReady: function () {// 页面渲染完成
  },
  onLoad: function (options) {
    var that = this; //调用应用实例的方法获取全局数据

    common.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      });
      common.post('My&a=withdraw_list', {}, "setOneContent", that);
    });
	common.changeCurrency(this)
  },
  components: {},
  props: {},
  methods: {
    inputBg: function () {
      this.setData({
        inputBg: this.inputBg == 'white' ? '#ebebeb' : 'white'
      });
    },
    inputBg1: function () {
      this.setData({
        inputBg1: this.inputBg1 == 'white' ? '#ebebeb' : 'white'
      });
    },
    formSubmit: function (e) {
      console.log(e.detail);

      if (e.detail.value.truename == '') {
        wx.showModal({
          title: '提示',
          content: "请输入姓名",
          showCancel: false
        });
        return false;
      } else if (isNaN(e.detail.value.money) || e.detail.value.money == '') {
        wx.showModal({
          title: '提示',
          content: "请输入合法的金额",
          showCancel: false
        });
        return false;
      } else if (parseFloat(e.detail.value.money) > parseFloat(this.userInfo.can_withdraw_money)) {
        wx.showModal({
          title: '提示',
          content: "单次充值金额最高不能超过" + this.userInfo.can_withdraw_money + "元",
          showCancel: false
        });
        return false;
      } else if (e.detail.value.money < 1) {
        wx.showModal({
          title: '提示',
          content: "单次提现金额最低不能低于 1 元",
          showCancel: false
        });
        return false;
      }

      common.post('My&a=withdraw', {
        money: e.detail.value.money,
        truename: e.detail.value.truename
      }, "saveResult", this);
    },
    saveResult: function (result) {
      wx.showModal({
        title: '提示',
        content: "提现申请成功，请等待管理员审核！如果真实姓名不对，将会驳回申请。",
        showCancel: false
      });
      this.setData({
        input: ''
      });
      app.globalData.userInfo = null;
      this.onLoad();
    },
    setOneContent: function (result) {
      console.log(result);
      this.setData({
        withdraw_list: result.withdraw_list
      });
      wx.hideToast();
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
/* pages/my/money_withdraw.css */
.modify{ padding: 10rpx 20rpx; background: #fff; border-bottom: #f1f1f1 1rpx solid;}
.modify input{ font-size: 30rpx; height: 28rpx;  padding:10rpx;   }
.remind{ padding:30rpx 20rpx 0 20rpx; line-height: 1;}
.inner{ background: #fff; margin: 30rpx 0; font-size: 24rpx;}
.inner .item{  display: flex; align-items: center; -webkit-box-align: center; height: 80rpx; line-height: 80rpx; border-bottom: #f1f1f1 2rpx solid; }
.inner .item .date{  flex: 1; -webkit-box-flex: 1; text-align: center; }
</style>